<?php

namespace app\api\logic;

use app\api\model\UserCollection;
use app\basic\BaseLogic;

class UserCollectionLogic extends BaseLogic
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new UserCollection();
    }
    /**
     * 获取收藏统计
     * @param int $userId 用户ID
     * @return array
     */
    public function getCollectionCount($userId)
    {
        // 获取总收藏数
        $totalCount = $this->model->where('user_id', $userId)
            ->count();

        // 获取本月收藏数
        $monthCount = $this->model->where('user_id', $userId)
            ->whereMonth('create_time')
            ->count();

        return [
            'month_count' => $monthCount,
            'total_count' => $totalCount
        ];
    }

    /**
     * 获取收藏列表
     * @param int $userId 用户ID
     * @param int $page 页码
     * @param int $limit 每页数量
     * @return array
     */
    public function getCollectionList($userId, $page, $limit)
    {
        $query = $this->model->where('user_id', $userId);
        
        // 获取总数
        $total = $query->count();
        
        // 获取列表数据
        $list = $this->model->where('user_id', $userId)
            ->field('id, article_id, title, create_time')
            ->order('create_time', 'desc')
            ->page($page, $limit)
            ->select()
            ->each(function($item) {
                // 使用 is_numeric 判断时间戳是否为数字
                if (is_numeric($item['create_time'])) {
                    $item['create_time'] = date('Y-m-d H:i:s', (int)$item['create_time']);
                }
                return $item;
            })
            ->toArray();

        return [
            'list' => $list,
            'total' => $total,
            'page' => (int)$page,
            'limit' => (int)$limit
        ];
    }

    /**
     * 检查文章是否已收藏
     * @param int $userId 用户ID
     * @param int $articleId 文章ID
     * @return bool
     */
    public function isCollected($userId, $articleId)
    {
        return $this->model->where('user_id', $userId)
            ->where('article_id', $articleId)
            ->count() > 0;
    }

    /**
     * 添加收藏
     * @param int $userId 用户ID
     * @param int $articleId 文章ID
     * @param string $title 文章标题
     * @return bool
     */
    public function addCollection($userId, $articleId, $title)
    {
        $data = [
            'user_id' => $userId,
            'article_id' => $articleId,
            'title' => $title,
            'create_time' => time()
        ];

        return $this->model->insert($data) !== false;
    }

    /**
     * 批量删除收藏
     * @param int $userId 用户ID
     * @param array $ids 收藏ID数组
     * @return bool
     */
    public function deleteCollection($userId, $ids)
    {
        return $this->model->where('user_id', $userId)
            ->whereIn('id', $ids)
            ->delete() !== false;
    }

    /**
     * 批量删除收藏
     * @param int $userId 用户ID
     * @param array $ids 收藏ID数组
     * @return bool
     */
    public function deleteCollectionId($userId, $ids)
    {
        return $this->model->where('user_id', $userId)
            ->where('article_id', $ids)
            ->delete() !== false;
    }
} 